package at.fhj.itm10.eventgallery;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class ManualActivity extends Activity {
	
	private TextView eventCode;
	private TextView subTitle;
	private Button sendButton;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manual);
        
        eventCode = (TextView) findViewById(R.id.man_eventCode);
        subTitle = (TextView) findViewById(R.id.man_subTitle);
        sendButton = (Button) findViewById(R.id.man_sendButton);
        
        // acts when somebody hits enter on soft-keyboard
        eventCode.setOnEditorActionListener(new OnEditorActionListener() {
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_GO) {
					Log.d("goButton", "button clicked");
					goBack();
				}
				return false;
			}
		});
        
        // acts when somebody hits send button
        sendButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				goBack();
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_manual, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    private void goBack() {
    	String event = eventCode.getText().toString();
    	if (event.length() != 6) {
    		subTitle.setText(getResources().getString(R.string.man_string_err));
    		subTitle.setTextColor(getResources().getColor(R.color.msgColor));
    		return;
    	}
    	
    	Log.d("goBack", "go back to mainActivity");
		Intent resultIntent = ManualActivity.this.getIntent();
		resultIntent.putExtra("EVENT_CODE", eventCode.getText().toString());
		setResult(Activity.RESULT_OK, resultIntent);
		finish();
	}
}
